package com.wicket.okrcomponent.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrcomponent.base.service.MOmsMessageMemberChannelRelationService;
import com.wicket.okrcomponent.base.service.dto.req.*;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.common.tools.StringUtils;
import com.wicket.okrcomponent.common.util.CommonFunctionHelper;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsMessageMemberChannelRelationMapper;
import com.wicket.okrcomponent.dal.po.mbg.OmsMessageLocationRemind;
import com.wicket.okrcomponent.dal.po.mbg.OmsMessageMemberChannelRelation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
//import com.wicket.okrcomponent.base.service.dto.res.*;

import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsMessageMemberChannelRelationServiceImpl
  extends ServiceImpl<OmsMessageMemberChannelRelationMapper, OmsMessageMemberChannelRelation>
  implements MOmsMessageMemberChannelRelationService {

  @Autowired
  OmsMessageMemberChannelRelationMapper omsMessageMemberChannelRelationMapper;
/**
   * 2-5-09查询成员通道详情[6807]
   * gen by moon at 5/17/2023, 3:41:07 AM
   */
 @Trace(operationName = "2-5-09查询成员通道详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  OmsMessageMemberChannelRelation  queryMemberChannelDetail(QueryMemberChannelDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<OmsMessageMemberChannelRelation> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessageMemberChannelRelation> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                OmsMessageMemberChannelRelation retData = retList.get(0);
                
                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }
/**
   * 2-5-09新增成员通道[6810]
   * gen by moon at 8/15/2023, 8:32:03 PM
   */
 @Trace(operationName = "2-5-09新增成员通道")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addMemberChannel(OmsMessageMemberChannelRelation omsMessageMemberChannelRelation) {
     // TODO: 2024/1/19
     if(!StringUtils.isEmpty(omsMessageMemberChannelRelation.getCounterpartChannelLogo())){
         omsMessageMemberChannelRelation.setCounterpartChannelLogo(CommonFunctionHelper.replaceFilePath(omsMessageMemberChannelRelation.getCounterpartChannelLogo()));
     }
     omsMessageMemberChannelRelation.setMemberChannelRelationId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
omsMessageMemberChannelRelation.setIsValid("FALSE");
     if(super.save(omsMessageMemberChannelRelation)){
        return omsMessageMemberChannelRelation.getMemberChannelRelationId();
     }else {
        return "-1";
     }
    
  }
/**
   * 2-5-09查询成员通道列表[7011]
   * gen by moon at 8/6/2023, 6:13:57 PM
   */
 @Trace(operationName = "2-5-09查询成员通道列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsMessageMemberChannelRelation>  queryMemberChannelList(QueryMemberChannelListReq reqDto) {

        try {
            QueryWrapper<OmsMessageMemberChannelRelation> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            // TODO: 2024/1/17
            if(reqDto.getUnReadCounts()!=null){
                queryWrapper.gt("un_read_counts",reqDto.getUnReadCounts());
                reqDto.setUnReadCounts(null);
            }

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageMemberChannelRelation.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageMemberChannelRelation.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageMemberChannelRelation.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            // TODO: 2024/5/30
            List<OmsMessageMemberChannelRelation> retList = super.list(queryWrapper);
            for(OmsMessageMemberChannelRelation one : retList){
                one.setCounterpartChannelLogo(CommonFunctionHelper.getFilePath(one.getCounterpartChannelLogo()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 2-5-09修改成员通道[7014]
   * gen by moon at 5/31/2023, 1:40:01 AM
   */
 @Trace(operationName = "2-5-09修改成员通道")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateMemberChannel(OmsMessageMemberChannelRelation omsMessageMemberChannelRelation) {
// TODO: 2024/1/19
     if(!StringUtils.isEmpty(omsMessageMemberChannelRelation.getCounterpartChannelLogo())){
         omsMessageMemberChannelRelation.setCounterpartChannelLogo(CommonFunctionHelper.replaceFilePath(omsMessageMemberChannelRelation.getCounterpartChannelLogo()));
     }
        return super.updateById(omsMessageMemberChannelRelation);
  }
/**
   * 2-5-09批量增成员通道[7035]
   * gen by moon at 8/15/2023, 8:32:03 PM
   */
 @Trace(operationName = "2-5-09批量增成员通道")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchAddMemberChannel(List<OmsMessageMemberChannelRelation> omsMessageMemberChannelRelation) {

      if(omsMessageMemberChannelRelation==null || omsMessageMemberChannelRelation.size()==0){
        return false;
      }

      return super.saveBatch(omsMessageMemberChannelRelation.stream().map(item->{
          // TODO: 2024/1/19
          if(!StringUtils.isEmpty(item.getCounterpartChannelLogo())){
              item.setCounterpartChannelLogo(CommonFunctionHelper.replaceFilePath(item.getCounterpartChannelLogo()));
          }

       item.setMemberChannelRelationId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
item.setIsValid("FALSE");
          return item;
      }).collect(Collectors.toList()));
  }
/**
   * 2-5-08删除成员通道[7046]
   * gen by moon at 5/31/2023, 6:08:12 PM
   */
 @Trace(operationName = "2-5-08删除成员通道")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteMemberChannel(String memberChannelRelationId ) {

      return super.removeById(memberChannelRelationId);
  }
/**
   * 2-5-09批量删成员通道[7051]
   * gen by moon at 5/31/2023, 6:08:13 PM
   */
 @Trace(operationName = "2-5-09批量删成员通道")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchDeleteMemberChannel(List<String> reqDto) {

          if(reqDto==null || reqDto.size()==0){
            return false;
          }

          return super.removeByIds(reqDto);
  }
/**
   * 2-5-09批量改成员通道[7052]
   * gen by moon at 5/31/2023, 6:08:13 PM
   */
 @Trace(operationName = "2-5-09批量改成员通道")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchUpdateMemberChannel(List<OmsMessageMemberChannelRelation> omsMessageMemberChannelRelation) {

      if(omsMessageMemberChannelRelation==null || omsMessageMemberChannelRelation.size()==0){
        return false;
      }
        for(OmsMessageMemberChannelRelation one : omsMessageMemberChannelRelation){
            // TODO: 2024/1/19
            if(!StringUtils.isEmpty(one.getCounterpartChannelLogo())){
                one.setCounterpartChannelLogo(CommonFunctionHelper.replaceFilePath(one.getCounterpartChannelLogo()));
            }
        }
        return super.saveOrUpdateBatch(omsMessageMemberChannelRelation);

  }
/**
   * 2-5-09批量查询成员通道[7060]
   * gen by moon at 5/31/2023, 7:59:18 PM
   */
 @Trace(operationName = "2-5-09批量查询成员通道")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsMessageMemberChannelRelation>  batchQueryMemberChannel(BatchQueryMemberChannelReq reqDto) {

        try {
            QueryWrapper<OmsMessageMemberChannelRelation> queryWrapper = new QueryWrapper<>();

            if(reqDto.getMemberChannelList()==null || reqDto.getMemberChannelList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("user_id", reqDto.getMemberChannelList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageMemberChannelRelation.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageMemberChannelRelation.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageMemberChannelRelation.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageMemberChannelRelation>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 2-5-09修改自动减成员通道消息数量[7367]
   * gen by moon at 8/11/2023, 7:13:58 PM
   */
 @Trace(operationName = "2-5-09修改自动减成员通道消息数量")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateAutoSubMemberMessageNum(OmsMessageMemberChannelRelation omsMessageMemberChannelRelation) {
     // TODO: 2023/12/27
     UpdateWrapper<OmsMessageMemberChannelRelation> uw = new UpdateWrapper<>();
     omsMessageMemberChannelRelation.setUnReadCounts(1L);
     omsMessageMemberChannelRelation.setOperateTime(CommonFunctionHelper.getCurrentTime());
     uw.eq("member_channel_relation_id",omsMessageMemberChannelRelation.getMemberChannelRelationId());
     if(omsMessageMemberChannelRelation.getUnReadCounts()!=null){
         uw.setSql("un_read_counts = un_read_counts - " + omsMessageMemberChannelRelation.getUnReadCounts());//ADD_NUMERICAL
         omsMessageMemberChannelRelation.setUnReadCounts(null);
     }


     return super.update(omsMessageMemberChannelRelation,uw);
  }
/**
   * 2-5-09修改自动加成员通道未读数[8207]
   * gen by moon at 12/27/2023, 3:48:54 AM
   */
 @Trace(operationName = "2-5-09修改自动加成员通道未读数")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateAutoAddUnReadCounts(OmsMessageMemberChannelRelation omsMessageMemberChannelRelation) {


     // TODO: 2023/12/27  
     UpdateWrapper<OmsMessageMemberChannelRelation> uw = new UpdateWrapper<>();
     omsMessageMemberChannelRelation.setUnReadCounts(1L);
     uw.eq("member_channel_relation_id",omsMessageMemberChannelRelation.getMemberChannelRelationId());
     if(omsMessageMemberChannelRelation.getUnReadCounts()!=null){
         uw.setSql("un_read_counts = un_read_counts + " + omsMessageMemberChannelRelation.getUnReadCounts());//ADD_NUMERICAL
         omsMessageMemberChannelRelation.setUnReadCounts(null);
     }


     return super.update(omsMessageMemberChannelRelation,uw);

  }
}
